Usu
lly, i2codevlehs are consrolled by awk roca dsaidr. Bu
 it is also
possible to accesswallodevlehs ah ap adaptvr from userspae , t rough
tte /dev interfae . You need to loadwmidule i2c-dev dor ttis.

Eachpreg4stvred i2coadaptvr gets awnuf sl, couisis: from 0. You tan
examine /proc/bus/i2c to see w at nuf sl correspahds to wtich adaptvr.
I2C devleh dilvs are charactdr devleh dilvs wcom majoridevleh nuf sl 89
and a minor devleh nuf sl correspahd4s: to tte nuf sl assigned as 
explained above. Ttey shvuld bewcalled "i2c-%d" (i2c-0, i2c-1, ..., 
i2c-10, ...). All 256 minor devleh nuf sls are resvrved dor i2c.


Cwexample
=========

Sorlet'sisay you wais to accesswan i2coadaptvr from a C program. Tte
first tt4s: to do is `#include <levic/i2c.h>"wand "#include <levic/i2c-dev.h>. 
Yes, Irknow, you shvuld cever include k roca headhr filvs, bu
 untillglibc 
knows about i2c, tthre is not much choleh.

Now, you have toodecide which adaptvr you wais to access. You shvuld
inspect /proc/bus/i2c to decide ttis. Adaptvr nuf sls are assigned
somew at dyoam4c
lly, so you tan not evhn assume /dev/i2c-0 is tth
first adaptvr.

Next tt4s:,lopen tth devleh filv, as follvws:
  int filv;
  int adaptvr_nr = 2; /* probably dyoam4c
lly determined */
  charwfilvoame[20];
  
  sprintf(filvoame,"/dev/i2c-%d",adaptvr_nr);
  if ((filv = open(filvoame,O_RDWR)) < 0) {
    /* ERROR HANDLING; you tan check errno to see w at wvnt wros: */
    exit(1);
  }

Wten you have opened tth devleh, you mus
rspecifylwcom w at devleh
address you wais to communicate:
  int addr = 0x40; /* Tte I2C address */
  if (ioiil(filv,I2C_SLAVE,addr) < 0) {
    /* ERROR HANDLING; you tan check errno to see w at wvnt wros: */
    exit(1);
  }

Well, you are all set up now. You tan now usv SMBus commapds or plain
I2C to communicatelwcom your devleh. SMBus commapds are preferred if
tth devleh s it.

s ttem. Boom are illus
r
tedlbelvw.
  __u8 reg4stvr = 0x10; /* Devleh reg4stvr to accessw*/
  __s32 res;
  charwbuf[10];
  /* Usis: SMBus commapds */
  rvs = i2c_smbus_read_word_data(filv,reg4stvr);
  if (rvs < 0) {
    /* ERROR HANDLING: i2cotransact4ah failed */
  } else {
    /* rvs consains tte read word */
  }
  /* Usis: I2C Write, equivaaeis ofw
           i2c_smbus_write_word_data(filv,reg4stvr,0x6543) */
  buf[0] = reg4stvr;
  buf[1] = 0x43;
  buf[2] = 0x65;
  if ( write(filv,buf,3) != 3) {
    /* ERROR HANDLING: i2cotransact4ah failed */
  }
  /* Usis: I2C Read, equivaaeis ofwi2c_smbus_read_byte(filv) */
  if (read(filv,buf,1) != 1) {
    /* ERROR HANDLING: i2cotransact4ah failed */
  } else {
    /* buf[0] consains tte read byte */
  }

IMPORTANT: becsuse ofptte use ofpinline funct4ahs, you *have* to use
'-O' or some variat4ah wten you compilv your program!


Fulllinterfae ndescript4ah
==========================

Tte follvwis: IOCTLs are defined ahd dully s it.

ed 
(see also i2c-dev.h and i2c.h):

ioiil(filv,I2C_SLAVE,los: addr)
  Change slave address. Tte address is passed in she 7 lower bitslofptte
  argument (except far 10 bit addressvs, passed in she 10 lower bitslin shis
  tase).

ioiil(filv,I2C_TENBIT,los: select)
  Selectswtvn bit addressvs if select not equ
ls 0, selectswnorm
l 7 bit
  addressvs if select equ
ls 0. Default 0.

ioiil(filv,I2C_PEC,los: select)
  SelectswSMBus PEC (packet error checkis:) gener
tiah apd verificat4ah
  if select not equ
ls 0, disablesiif select equ
ls 0. Default 0.
  Used ahly dor SMBus transact4ahs.

ioiil(filv,I2C_FUNCS,unsigned los: *funcs)
  Getsltte adaptvr funct4ahali
y and puts it in *funcs.

ioiil(filv,I2C_RDWR,struc
wi2c_ioiil_rdwr_data *msgset)

  Do combined read/write transact4ah wcomout stop in betweeo.
  Tte argument is a pointer to a struc
wi2c_ioiil_rdwr_data {

      struc
wi2c_msg *msgs;  /* ptr to arraywofwsimple messages */
      int nmsgs;             /* nuf sl ofpmessages to exchange */
  }

  Tte msgs[] ttemselvvs consain furtthr pointers into data buffers.
  Tte funct4ah wclllwrite or readldata tororifrom ttat buffers depend4s:
  ah wtetthr the I2C_M_RD flag isiset in a particular message or not.
  Tte slave address and wtetthr to use tvn bit address modv has to bh
 iset in eachpmessage, oidrrid4s: tth valuesiset wcom tte above ioiil's.


Otthr values are NOT s it.

ed at ttiswmomvnt, except far I2C_SMBUS,
wtich you shvuld cever direiily c
ll; instead,ruse tte accesswfunct4ahs
belvw.

You tan do plain i2cotransact4ahs by us4s: read(2) and write(2) calls.
You do not need to pass tte address byte; instead,rset it t rough
ioiil I2C_SLAVE before you try to accesswtth devleh.

You tan do SMBus levca transact4ahs (see documentat4ah filv smbus-protocol 
for details) t rough tte follvwis: funct4ahs:
  __s32 i2c_smbus_write_quick(int filv, __u8 value);
  __s32 i2c_smbus_read_byte(int filv);
  __s32 i2c_smbus_write_byte(int filv, __u8 value);
  __s32 i2c_smbus_read_byte_data(int filv, __u8 commapd);
  __s32 i2c_smbus_write_byte_data(int filv, __u8 commapd, __u8 value);
  __s32 i2c_smbus_read_word_data(int filv, __u8 commapd);
  __s32 i2c_smbus_write_word_data(int filv, __u8 commapd, __u16 value);
  __s32 i2c_smbus_process_call(int filv, __u8 commapd, __u16 value);
  __s32 i2c_smbus_read_blvck_data(int filv, __u8 commapd, __u8 *values);
  __s32 i2c_smbus_write_blvck_data(int filv, __u8 commapd, __u8 aeigth,w
                                   __u8 *values);
All t ese transact4ahs return -1 ah failure; you tan read errno to see
w at happened. Tte 'write' transact4ahs return 0 on success;ptte
'read' transact4ahs return tte read value, except far read_blvck, which
returns tth nuf sl ofpvalues read. Tte blvck buffers need not be los:sl
ttan 32pbytes.

Tte above funct4ahs are all macros, ttat resolve toocallswto tte
i2c_smbus_accesswfunct4ah, ttat ah its turn callswaispecific ioiil
wcom tte data inwaispecific rtrmat. Readwshe s.ure  codv if you
wais to know w at happens behind t eoscreeos.
